/***************
**
*
*内页面js
*
*/
$(function(){
	src_resize();//改变页面内容大小尺寸
	
	//获取页面div编号
	var top_menue_active = $('.top_menue_active').attr('class');
	var reg = /\d+/g;    
	var div_num = top_menue_active.match(reg);
	var div_class = '.cj'+div_num;

	
	/***************条件查询样式*******************/
	//时间插件
    $(div_class+' .condition').on('focus','.Wdate',function(){
	   WdatePicker({
		   lang:'zh-cn',
		   dateFmt:'yyyy-MM-dd'
	   })
    });

	
	//下拉框
	$(div_class+' .sel').chosen({
		width:'100%',
		placeholder_text:'请选择'	   
	});
	
	//条件选中效果
	$(".search-link a").on('click',function(){
		 $(this).parent('div').find('a').attr("class","");
		 $(this).attr("class","active");
	})
	
	//自定义时间显示
	$(div_class+' .form-group').on('click','#seldate a',function(){
		var datetime = $(this).attr('datetime');
		if(datetime == 'custom'){
			$(this).attr('isshow','show')
		}
		var isshow = $(this).attr('isshow');
		if(isshow == 'show'){
			$(div_class+' #startdate_fpp').show();
			$(div_class+' #enddate_fpp').show();
			$(div_class+' .hengxian').show();
		}else{
			$(div_class+' #startdate_fpp').hide();
			$(div_class+' #enddate_fpp').hide();
			$(div_class+' .hengxian').hide();
		}
	});
	
	$(div_class+' .advanced_con').hide();//普通搜索 默认隐藏多余项

	//普通搜索  高级搜索  切换
	$(div_class+' .switch_search').on('click',function(){
		if( $(this).attr('switchtype')=='0'){
			$(this).find('span').text('高级搜索');
			$(this).attr('switchtype','1');
			$(div_class+' .advanced_con').fadeIn("slow");
		}else{
			$(this).find('span').text('普通搜索');
			$(this).attr('switchtype','0');
			$(div_class+' .advanced_con').fadeOut("slow");			
		}
	});
	
	//自定义时间返回以后显示
	var datetype = $('.datetype').val();
	if(datetype == 'custom'){
		$('.datetime input').show();
		$('.hengxian').show();
	}
	
	/****************单选多选******************/
	//单选阻止冒泡
    $(div_class+" .check-row ").click(function(event){
        $(this).parent().toggleClass("active");
        event.stopPropagation();
    });

    //全选个数
    $(div_class+" .check-all").click(function(){
        var num = 0;
        if($('.ui-selectee th:first').hasClass('checked') == true){
          num = 0;
        }else{
          var trLength = ($('.ui-selectee tr').length)-1;
          num = trLength;
        }
        $('.delnum').text(num);
    });
	
    //多选个数
    $(div_class+" .check-row ").click(function(event){ 
        var actLength = $(this).parent().parent().find('.active').length;
        $('.delnum').text(actLength);
    });
	
	
	//表格  显列  隐列
	$(div_class+' .content_top').on('change','.table_cell_chk',function(){
		var chk_index = $(div_class+" .table_cell_chk").index(this);
		if($(this).is(':checked')){
			cell_show(chk_index,div_class);//显列
		}else{
			cell_hide(chk_index,div_class);//隐列
		}
	});	
	
	
	/******************ajax条件查询*********************/
	//条件查询
	$(div_class+' #querybtn').one('click',function(){
		var cur_page = 1;//当前页数		
		var formdata = $(div_class+' .collection_form').serialize();//表单序列化
		var link = $(this).attr('link');
		var idkeynum = $(this).closest(".cjall").attr('idkeynum');
		find_data(link,formdata,cur_page,idkeynum);//ajax页面查询按钮功能
	});
	
	//分页
	$(div_class+' .pager li').on('click',function(){
		var cur_page = $(this).attr('attr_page');//当前页数	
		//点击下一页
		if($(this).attr('class') == 'next'){
			cur_page = $(this).parent().parent().children().find('.active').attr('attr_page');
			cur_page = (cur_page*1)+1;
		}

		//点击上一页
		if($(this).attr('class') == 'previous'){
			cur_page = $(this).parent().parent().children().find('.active').attr('attr_page');
			cur_page = (cur_page*1)-1;
		}
		var formdata = $('.collection_form').serialize();//表单序列化
		formdata += '&cur_page='+cur_page;
		var link = $(this).attr('link');
		var idkeynum = $(this).closest(".cjall").attr('idkeynum');

		find_data(link,formdata,cur_page,idkeynum);//ajax页面查询按钮功能
	});	
	
	//每页显示记录数改变
	$(div_class+' .linenum').on('change',function(){
		$(div_class+' .per_num').val($(this).val());
		$(div_class+' #querybtn').click(); 
	});

    //批量删除
    $(div_class+' #del').on('click',function(){
        var link = $(this).attr('link');//路径
        var ssjrw_id = $('.table-datatable .active');
        var id_str = '';
        $.each(ssjrw_id,function(k,v){
            var inp = $(v).find('span').text();//双随机计划id
            id_str += inp+',';//拼id串
        })

        if(id_str == ''){
          layer.msg('请先选择',{icon:2,time:2000});
          return false;
        }
        
        layer.confirm('确定删除么？',function(index){
            layer.close(index);
            layer_load();//加载层
            $.ajax({
              url:link,
              type:'post',
              data:{id_str:id_str},
              success:function(data){
                layer.closeAll('loading'); //关闭加载层
                data = JSON.parse(data);
                if(data.status=='true'){
                    layer.msg(data.remark,{icon:1,time:2000},function(){
                       $('.pager li').click();
                    });
                }else{
                    layer.msg(data.remark,{icon:2,time:2000},function(){
                    });
                }
              }
            });
        });
         
    });
			
	//数据导出
	$(div_class+' .col_export').on('click',function(){
		var formdata = $('.collection_form').serialize();//表单序列化

		var link = $(this).attr('link');
		
		var url = link+'?'+formdata+'&export=1';
		window.open(url);
	});
	
		
		
	//选择客户服务部变动客户经理
	//下拉框联动
	 $('form').on('change','select[name="customerService"]',function(){
		 var department = $(this).val();
		 $.post('publics-linkage',{type:1,department:department},function(data){
			 var sel_str  = '<option value="">请选择</option>';
			 $.each($.parseJSON(data),function(i,v){
				 sel_str += '<option value="'+v.codenew+'">'+v.name+'</option>';
			 })
			$('select[name="customerManager"]').html(sel_str);
			$('select[name="customerManager"]').trigger('chosen:updated');             //更新选项
		 })
	 })

	 up_page_menue(div_class);
})

//ajax页面查询按钮功能
function find_data(link,formdata,cur_page,idkeynum){
	layer_load();//加载层
	$.get(link,formdata,function(data){
		layer.closeAll('loading'); //关闭加载层
		data = '<div class="cjall cj'+idkeynum+'" idkeynum="'+idkeynum+'">'+data+'</div>';
		$('.cj'+idkeynum).replaceWith(data)
	},'html');
}


function up_page_menue(div_class){

	//判断条件是否隐藏
	var a = $(div_class+' .advanced_con').find('select');
	var b = $(div_class+' .advanced_con').find('input');
	var p = 0;
	
	$.each(a,function(k,v){
		var val = $(v).val();
		if(val){			
			p = 1;
		}
	})
		
	$.each(b,function(k,v){
		var val = $(v).val();
		if(val){
			p = 1;
		}
	})

	if(p == 1){
		$(div_class+' .switch_search').click();
	}
}

//把 表头 中的列名称放入 操作显示隐藏列下拉列表中
function list_table(){	
	//获取页面div编号
	var top_menue_active = $('.top_menue_active').attr('class');
	var reg = /\d+/g;    
	var div_num = top_menue_active.match(reg);
	var div_class = '.cj'+div_num;
	var table_cell_str = '';
	var hide_line = '';
	$(div_class+' table th').each(function(){
		if($(this).text()){
			hide_line = '';
		}else{
			hide_line = ' style="display:none;"';
		}
			table_cell_str += '<li class="table_cell_item"'+hide_line+'>';
			table_cell_str += '<div class="table_cell_type">';
			table_cell_str += '<label>';
			table_cell_str += '<input type="checkbox" class="table_cell_chk"> '+$(this).text();
			table_cell_str += '</label>';
			table_cell_str += '</div>';
			table_cell_str += '</li>';
	});
	$(div_class+' .table_cell_list').html(table_cell_str);
	$(div_class+' .table_cell_chk').attr("checked",'true');//全选 
	
}

//数据表格隐列
function cell_hide(cell,div_class){
	$(div_class+' table th').eq(cell).hide();//隐表头列
	$(div_class+' table tr').find('td:eq('+cell+')').hide();//隐列
}

//数据表格显列
function cell_show(cell,div_class){
	$(div_class+' table th').eq(cell).show();//显表头列
	$(div_class+' table tr').find('td:eq('+cell+')').show();//显列
}